package com.example.mbl.service.impl;

import com.example.mbl.dao.EventMapper;
import com.example.mbl.entity.Event;
import com.example.mbl.interceptor.AppToken;
import com.example.mbl.result.PageResult;
import com.example.mbl.result.WebRestResult;
import com.example.mbl.result.business.EventPageResult;
import com.example.mbl.result.business.EventResult;
import com.example.mbl.service.EventService;
import com.example.mbl.util.Tools;
import com.example.mbl.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.geom.RectangularShape;
import java.util.Date;
import java.util.List;

@Service
public class EventServiceImpl implements EventService {
    @Autowired
    private EventMapper eventMapper;

    @Override
    public List<Event> searchItemsByKeyWord(String keyword) {
        return eventMapper.searchItemsByKeyWord(keyword);
    }

    @Override
    public WebRestResult addEVent(Event event) {
        WebRestResult result = new WebRestResult();
        event.setEventid(UUIDUtil.randomUUID());
        if(Tools.isEmpty(event.getFlag()))
            event.setFlag("0");
        event.setCreatedate(new Date());
        if(eventMapper.insert(event)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增事件失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteEvent(Event event) {
        WebRestResult result = new WebRestResult();
        if(eventMapper.deleteByPrimaryKey(event.getEventid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除事件失败");
        }
        return result;
    }

    @Override
    public WebRestResult updateEvent(Event event) {
        WebRestResult result = new WebRestResult();
        if(eventMapper.updateByPrimaryKeySelective(event)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新事件失败");
        }
        return result;
    }

    @Override
    public WebRestResult setEventVisible(Event event) {
        WebRestResult result = new WebRestResult();
        if(eventMapper.setEventVisible(event.getEventid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("设置失败");
        }
        return result;
    }

    @Override
    public PageResult<EventPageResult> getEventList(String eventtitle, String eventdate, String eventtype, int page, int pagesize, String eventday) {
        PageResult<EventPageResult> result = new PageResult<>();
        int start = (page-1) * pagesize;

        List<EventPageResult> list = eventMapper.getEventList(eventtitle,eventdate,eventtype,start,pagesize,eventday);
        if(list.size()>0){
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
        }
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }

    @Override
    public EventResult getEventItem(String eventid) {
        EventResult result = eventMapper.getEventItem(eventid);
        if(!Tools.isEmpty(result)){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取事件详情失败");
        }
        return result;
    }
}
